﻿// 角度计算、转换相关函数

using System;
using System.Collections.Generic;
using System.Text;

namespace AdjPlane
{
    public class Angle_t
    {
        // 输入角度，使之保持在0~360
        public static double Keep_0To360(double D)
        {
            while (D < 0 || D >= 360)
            {
                if (D < 0) { D += 360; }
                else { D -= 360; }
            }
            return D;
        }
        //矫正弧度
        public static double Keep_0To2Pi(double RD)
        {
            while (RD < 0 || RD >= 2 * Math.PI)
            {
                if (RD < 0) { RD += 2 * Math.PI; }
                else { RD -= 2 * Math.PI; }
            }
            return RD;
        }
        public static double Keep_0ToPi(double RD)
        {
            RD = Keep_0To2Pi(RD);
            if (RD > Math.PI)
                RD = 2*Math.PI-RD;
            return RD;
        }
        public static double GetAzi(Point2D A,Point2D B)
        {
            double dx = B.X - A.X;
            double dy = B.Y - A.Y;
            return GetAzi(dx, dy);
        }
        //坐标增量计算方位角
        public static double GetAzi(double dx, double dy)
        {
            double azi=0;
            if (dx == 0)
            {
                if (dy > 0) azi = Math.PI * 0.5;
                if (dy < 0) azi = Math.PI * 1.5;
                if (dy == 0) azi = 0;
                return azi;
            }
            if (dy == 0)
            {
                if (dx > 0) azi = 0;
                if (dx < 0) azi = Math.PI;
                return azi;
            }
            azi = Math.Atan(Math.Abs(dy / dx));//方位角,+x+y
            if (dx < 0 && dy > 0) { azi = Math.PI - azi; } //-x+y
            else if (dx < 0 && dy < 0) { azi += Math.PI; } //-x-y
            else if (dx > 0 && dy < 0) { azi = 2 * Math.PI - azi; } //+x-y
            return azi;
        }


        public static double DMS2D(double dms)// 度分秒转度
        {
            double d = (int)dms;
            double m = (int)((dms - d) * 100);
            double s = ((dms - d) * 100 - m) * 100;
            double D = d + m / 60 + s / 3600;
            return D;
        }
        public static double DMS2RD(double dms) //（如1.23456表示1°23′45.6″）
        {// 度分秒转弧度
            double D = DMS2D(dms);
            return D * Math.PI / 180;
        }
        public static double D2DMS(double D)
        {
            double d = (int)D;
            double m = (int)((D - d) * 60);
            double s = ((D - d) * 60 - m) * 60;
            double dms = d + m / 100 + s / 10000;
            return dms;
        }
        public static double RD2D(double RD)
        {
            return RD * 180 / Math.PI;
        }
        public static double D2RD(double D)
        {
            return D * Math.PI / 180;
        }
        public static double RD2DMS(double RD)
        {
            double dms = D2DMS(RD * 180 / Math.PI);
            return dms;
        }
    }
}
